<template>
	<view class="container">
		<view v-if="list.length" class="coupon-list">
			<view class="coupon-item" v-for="(item, index) in list" :key="index">
				<view class="item-wrapper"
					:class="item.check == 1 ? 'color-' +  COMMON.color[index % COMMON.color.length] : 'gray'">
					<view class="coupon-type">满减券</view>
					<view class="tip">
						<view>
							<text class="money">￥{{item.reduce_price}}</text>
						</view>
						<text class="pay-line">满{{item.min_price}}元可用</text>
					</view>
					<view class="split-line"></view>
					<view class="content">
						<view class="title">{{item.name}}</view>
						<view class="bottom">
							<view class="time">
								<text v-if="item.expire_type == 20">{{ item.start_time }}~{{ item.end_time }}</text>
								<text v-else>领取{{item.expire_day}}天内有效</text>
							</view>
							<text class="time">限领{{item.limit_quantity}}张</text>
							<view class="receive" v-if="item.check == 1" @click="receive_coupon(item.coupon_id)">
								<text>立即领取</text>
							</view>
							<view class="receive" v-else-if="item.check == 2"><text>已领取</text></view>
							<view class="receive" v-else><text>该券已领完</text></view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="u-empty" v-else>
			<u-empty text="暂无数据"
				icon="https://tianliyx.oss-cn-shanghai.aliyuncs.com/images/20250311/0057423001741660320.png"></u-empty>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';
	import {
		onShow,
		onShareAppMessage
	} from '@dcloudio/uni-app';
	import {
		COMMON
	} from "../../utils/commom.js"
	import {
		useAuthStore
	} from '@/stores/user.js';
	import {
		get_coupon,
		add_coupon
	} from "../../api/coupon.js"

	const list = ref([]);
	const authStore = useAuthStore();

	onShow(() => {
		authStore.initialize();
		if (!authStore.isLogin) {
			COMMON.toLogin();
			return
		}
		get_list();
	});

	function get_list() {
		get_coupon({
			is_private: 2
		}).then(res => {
			let data = res.data;
			list.value = data;
		})
	}

	function receive_coupon(coupon_id) {
		add_coupon(coupon_id).then(res => {
			uni.showToast({
				title: res.msg,
				icon: "none",
				success() {
					setTimeout(function() {
						get_list();
					}, 1000);
				}
			});
		})
	}
	
	// 分享到聊天
	onShareAppMessage(() =>{
		return {
			title: '优惠券',
			path: "/pages/coupon/promotion"
		}
	})
</script>

<style lang="scss" scoped>
	@import "@/static/css/coupon.scss";
</style>